package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.*;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page> show(int page,int pageSize){
        //构造分页器
        Page page1 = new Page<>(page, pageSize);
        //编写条件构造器
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        lambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        //添加排序条件
        lambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        //分页查询
        orderService.page(page1,lambdaQueryWrapper);
        return R.success(page1);
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number){
        //1).构造分页构造器
        Page page1 = new Page(page, pageSize);
        //2).构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //3).添加过滤条件
        queryWrapper.eq(number!=null,Orders::getNumber,number);
        //4).添加排序条件

        LambdaQueryWrapper<Orders> desc = queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(page1,queryWrapper);
        //5)执行查询
        /*List<Orders> list1 = orderService.list(queryWrapper);
        ArrayList<Long> longs = new ArrayList<>();
        for (Orders orders : list1) {
            longs.add(Long.valueOf(orders.getNumber()));
        }
        log.info("------------订单集合------------"+longs);
        LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(OrderDetail::getOrderId,longs);
        orderDetailService.page(page1,lambdaQueryWrapper);*/
        return R.success(page1);
    }
}
